package com.lemner.ke.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.annotation.DimenRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.lemner.ke.R;

import java.util.List;

/**
 * 文件名：BaseAdapter
 * 描  述：Adapter基类
 * 作  者：raintea修改(xRec可能用不用到)
 * 时  间：2018.8.8 2018.10.8
 */

public abstract class BaseAdapter<T> extends RecyclerView.Adapter {
    /*---------------------------- 初始化系列方法 ----------------------------*/
    public Context context;
    public LayoutInflater layoutInflater;
    public List<T> list;

    public BaseAdapter(Context context) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    /**
     * 设置数据
     */
    public void setList(List<T> list) {
        if (list != null && list.size() != 0) {
            this.list = list;
            notifyDataSetChanged();
        }
    }

    /**
     * 追加设置数据
     */
    public void addList(List<T> newList) {
        if (newList != null && newList.size() != 0) {
            if (list == null || list.size() == 0) {
                setList(newList);
            } else {
                int oldList = list.size();
                list.addAll(newList);
                notifyItemRangeInserted(oldList, newList.size());
            }
        }
    }

    @NonNull
    @Override
    public abstract RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType);
//这没看懂hiker2.0中也没有先注释下     9.25-张恺
//    public abstract void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position, final T bean);
//
//    @Override
//    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
//        T bean = list.get(position);
//        onBindViewHolder(holder, position, bean);
//    }

    /**
     * 获取数据个数
     */
    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    /**
     * 获取item的位置
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获取item的位置
     */
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    /*---------------------------- startActivity系列方法 ----------------------------*/

    /**
     * 页面跳转
     *
     * @param clz，目标Activity
     */
    public void startActivity(Class<?> clz) {
        startActivity(clz, null);
    }

    /**
     * 带数据的页面跳转
     *
     * @param clz，目标Activity
     */
    public void startActivity(Class<?> clz, Intent intent) {
        if (intent != null) {
            intent.setClass(context, clz);
        } else {
            intent = new Intent(context, clz);
        }
        context.startActivity(intent);
    }

    /**
     * finish关闭页面
     */
    public void finish() {
        ((Activity) context).finish();
        ((Activity) context).overridePendingTransition(R.anim.anim_activity_left_in_slow, R.anim.anim_activity_right_out);
    }

    /*---------------------------- 输出toast和log ----------------------------*/

    /**
     * 输出Toast
     */
    public void showToast(String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 输出Log
     */
    public void showLog(String text) {
        Log.d("ClassName:" + getClass().getName(), text);
    }

    /*---------------------------- 隐藏/显示软键盘 ----------------------------*/

    /**
     * 隐藏软键盘
     */
    public void hideSoftKeyBoard() {
        Activity activity = (Activity) context;
        if (activity == null) {return;}
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive()) {
            imm.hideSoftInputFromWindow(activity.getWindow().peekDecorView().getWindowToken(), 0);
        }
    }

    /**
     * 显示软键盘
     */
    public void showSoftKeyBoard() {
        Activity activity = (Activity) context;
        if (activity == null) {return;}
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null && imm.isActive()) {
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }
    }

    /*---------------------------- 获取R.string内容 ----------------------------*/

    /**
     * 获取R.string内容
     *
     * @param id，string的id
     */
    @NonNull
    public String getStringForR(@StringRes int id) throws Resources.NotFoundException {
        return context.getResources().getString(id);
    }

    /**
     * 获取R.string内容
     *
     * @param id，string的id
     * @param formatArgs，参数列表
     */
    @NonNull
    public String getStringForR(@StringRes int id, Object... formatArgs) throws Resources.NotFoundException {
        return context.getResources().getString(id, formatArgs);
    }

    /*---------------------------- 获取R.dimens内容 ----------------------------*/

    /**
     * 获取R.dimens内容
     *
     * @param id，dimens的id
     * @return
     * @throws Resources.NotFoundException 特殊:获取sp:textView.setText(0,getDimensForR(R.dimens.xxx));
     */
    @NonNull
    public float getDimensForR(@DimenRes int id) throws Resources.NotFoundException {
        return context.getResources().getDimension(id);
    }
}
